/**
 * Created by Administrator on 2016/8/11.
 */
define(["require",'config/appregister',"business/vedio/services/httpServices","business/vedio/dir/http","business/workSkill/directive/startLevel"],function(require,app,httpServices){
    app.controller('vedioCtrl',function($scope,$http,$state,auth){
        $scope.updateCollection=function(){
            auth.isLogin().then(function(){
                updateCollection();
            })
        }
       $scope.goworkData=function(id){
			$state.go("eng.techdetail",{fileId:id})
		}
		$scope.goSkill=function(id){
			$state.go("eng.workDetailsCtrl",{skillId:id})
		}
		$scope.LinkCourse=function(id){
			$state.go("eng.vedioDetails",{courseId:id})
		}
		$scope.goanswer=function(id){
			$state.go("eng.postbardeta",{postbarId:id})
		}
		$scope.goNews = function(id){
			$state.go("eng.newsDetail",{newsId:id})
		}
		$scope.Linklibrary=function(id){
			$state.go("eng.libraryDetail",{thesisId:id})
		}
		$scope.Linkpostbar=function(id){
			$state.go("eng.libraryDetail",{thesisId:id})
		}
        /*星级*/
        $scope.$parent.isDetailPage = false;
        $scope.$parent.isMainpage = true;
        $scope.goTo = function(item){
            
           
            $state.go("eng.vedioDetails",{courseId:item.courseId})

        }
        $scope.linkSearch=function(name){
            $rootScope.GLOBALKEYWORD =  name;
            sessionStorage.searchKeyWord = name;
            $state.transitionTo("search.course", {}, {
                reload: true, inherit: false, notify: true
            });
        }
        $scope.max = 5;//最大星级
        $scope.ratingVal = 1;///HTML中也可传递(优先于js),默认星级，必需设置，下面同样名字覆盖，否则不显示；$
        $scope.readonly = true;//false可编辑，true不可编辑
        $scope.onHover = function(val){
            $scope.hoverVal = val;
        };
        $scope.onLeave = function(){
            $scope.hoverVal = null;
        }
        $scope.onChange = function(val){
            $scope.ratingVal = val;
        };
        $scope.listParams = {
            currentTcId: "",
            download: false,
            time: false,
            pageNo: 1,
            pageSize:10,
            all: false,
            typeId: "",
            scId: "",
            dcId: "",
            keyWord:"",
            //dcName scName 用于加入缓存 在详情中显示面包树导航
            dcName:"",
            scName:""
        };
        /*tab栏目切换*/
        $scope.color = 2;
        $scope.showCOn=2;
        $scope.tab=4;
        ONeClass()
        function ONeClass(){
            var req ={
                type:"GET",
                uri:"getTopCategory",
                data:{}
            };
            httpServices.getcourseCata(req).success(function(response){
                $scope.$apply(function(){
                    $scope.CityVideos = response.data;
                    $scope.oneId = response.data[0].tcId;
                    var req ={
                        type:"GET",
                        uri:"getSecondaryCategory?tcId=" + response.data[0].tcId,
                        data:{}
                    };
                    httpServices.getcourseCata(req).success(function(response){
                        $scope.$apply(function(){
                            $scope.WorkCousrse = response.data.list;
                        })
                    });

                    if(sessionStorage.topId != undefined && sessionStorage.twoId == undefined && sessionStorage.thrId == undefined){
                        $scope.tab = sessionStorage.topId;

                        var req ={
                            type:"GET",
                            uri:"getSecondaryCategory?tcId=" + sessionStorage.topId,
                            data:{}
                        };
                        httpServices.getcourseCata(req).success(function(response){
                            $scope.$apply(function(){
                                $scope.WorkCousrse = response.data.list;
                            })
                        });


                        var req ={
                            type:"GET",
                            uri:"getList",
                            data:{
                                pageNo:$scope.listParams.pageNo,
                                pageSize:$scope.pageSize,
                                tcId:sessionStorage.topId
                            }
                        };
                        httpServices.getcourse(req).success(function(response){
                            $scope.$apply(function(){
                                $scope.courseList = response.data.list;
                                $scope.startLevel = response.data.list.collectionCount;
                                $scope.totalItem = response.data.Pagination.totalCount;
                                getListModel2();
                            })
                        });

                    }else if(sessionStorage.topId != undefined && sessionStorage.twoId != undefined && sessionStorage.thrId == undefined ){
                        $scope.tab = sessionStorage.topId;
                        $scope.currentId =  sessionStorage.twoId;
                        $scope.color=1;
                        var req ={
                            type:"GET",
                            uri:"getSecondaryCategory?tcId=" + sessionStorage.topId,
                            data:{}
                    };
                        httpServices.getcourseCata(req).success(function(response){
                            $scope.$apply(function(){
                                $scope.WorkCousrse = response.data.list;
                            })
                        });


                        var req ={
                            type:"GET",
                            uri:"getThirdCategory",
                            data:{
                                scId:sessionStorage.twoId
                            }
                        };
                        httpServices.getcourseCata(req).success(function(response){
                            $scope.$apply(function(){
                                $scope.ThreeList = response.data;
                                	if(sessionStorage.topId == 4){
				                   		$scope.triangleone=2;
				                   	}else{
				                   		$scope.triangleone=1;
				                   	}
									var top = $("#ThreeCourseSwitch").position().top;
									var width = $(".TwocataColor").width()/2+$(".TwocataColor").offset().left-$('#thrilistCon').offset().left;
									$(".triangle").css({"left":width,"top":top-8})
                            })
                        });

                        var req ={
                            type:"GET",
                            uri:"getList",
                            data:{
                                pageNo:$scope.listParams.pageNo,
                                pageSize:$scope.pageSize,
                                tcId:sessionStorage.topId,
                                scId:sessionStorage.twoId
                            }
                        };
                        httpServices.getcourse(req).success(function(response){
                            $scope.$apply(function(){
                                $scope.courseList = response.data.list;
                                $scope.startLevel = response.data.list.collectionCount;
                                $scope.totalItem = response.data.Pagination.totalCount;
                                getListModel2();
                            })
                        });




                    }else if(sessionStorage.topId != undefined && sessionStorage.twoId != undefined && sessionStorage.thrId != undefined){
                        $scope.tab = sessionStorage.topId;
                            $scope.showCOn=1;
                            $scope.currentId =  sessionStorage.twoId;//二级id
                            $scope.currentId3 = sessionStorage.thrId;//三级id

                            var req ={
                                type:"GET",
                                uri:"getSecondaryCategory?tcId=" + sessionStorage.topId,
                                data:{}
                            };
                            httpServices.getcourseCata(req).success(function(response){
                                $scope.$apply(function(){
                                    $scope.WorkCousrse = response.data.list;
                                })
                            });


                            var req ={
                                type:"GET",
                                uri:"getThirdCategory",
                                data:{
                                    scId:sessionStorage.twoId
                                }
                            };
                            httpServices.getcourseCata(req).success(function(response){
                                $scope.$apply(function(){
                                    $scope.ThreeList = response.data;
                                    	if(sessionStorage.topId == 4){
					                   		$scope.triangleone=2;
					                   	}else{
					                   		$scope.triangleone=1;
					                   	}
										var top = $("#ThreeCourseSwitch").position().top;
										var width = $(".TwocataColor").width()/2+$(".TwocataColor").offset().left-$('#thrilistCon').offset().left;
										$(".triangle").css({"left":width,"top":top-8})
                                })
                            });

                        $scope.color=1;
                        var req ={
                            type:"GET",
                            uri:"getList",
                            data:{
                                pageNo:$scope.listParams.pageNo,
                                pageSize:$scope.pageSize,
                                tcId:sessionStorage.topId,
                                scId:sessionStorage.twoId,
                                thirdId:sessionStorage.thrId
                            }
                        };
                        httpServices.getcourse(req).success(function(response){
                            $scope.$apply(function(){
                                $scope.courseList = response.data.list;
                                $scope.startLevel = response.data.list.collectionCount;
                                $scope.totalItem = response.data.Pagination.totalCount;
                                getListModel2();
                            })
                        });

                    }else{
                        courseList();
                    }
                })
            });
        }
        /*一级分类*/
        /*默认显示1的课程列表*/
        function courseList(){
            var req ={
                type:"GET",
                uri:"getList",
                data:{
                    tcId:$scope.oneId,
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.pageSize
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel3();
                })
            });
        }


        /*热门1推荐课程数据*/
        function hotCourse(){
            var req ={
                type:"GET",
                uri:"getHotCourse?pageNo=1&pageSize=6&all=false",
                data:{}
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    var data = response.data.list;
                    $scope.hotCouse = data;
                    var order = "visitCount";
                    $scope.col = order;//默认按name列排序
                    $scope.desc = 0;

                })
            });

        };
        /*最近浏览数据*/
        function recentData(){
        	
        	var req ={
				type:"GET",
				uri:"getEnginBrowse?count=8",
				data:{}
			};
			httpServices.getWorkData(req).success(function(response){
				$scope.$apply(function(){
					$scope.recentDate = response.data.enginBrowseList;//竟然设计成全局的浏览情况，可怕的产品
	
				})
			});
        	
        	
      
        }
        recentData();
        hotCourse();
        $scope.secZy="专业分类:"
        $scope.tabones=function(item,$index){
        	if(item.tcName=="工程云大学"){
        		$scope.secZy="专业分类:"
        		$scope.showist=1
        	}else{
        		$scope.showist=2
        		$scope.secZy="专业分类:"
        	}
        	sessionStorage.removeItem("topId")
            sessionStorage.removeItem("twoId")
            sessionStorage.removeItem("thrId")
            $scope.ThreeList="";
            $scope.currentId=-1;
            $scope.tab =$scope.oneClassId = item.tcId;
            var req ={
                type:"GET",
                uri:"getList",
                data:{
                    tcId:$scope.oneClassId,
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.pageSize
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel3();
                })
            });


            var req ={
                type:"GET",
                uri:"getSecondaryCategory?tcId=" + item.tcId,
                data:{}
            };
            httpServices.getcourseCata(req).success(function(response){
                $scope.$apply(function(){
                    var data = response.data.list;
                    $scope.WorkCousrse = data;
                })
            });

        }
        /*三级分类点击切换显示*/
        $scope.opThrennSwitch = function(item,index){

            var req ={
                type:"GET",
                uri:"getList",
                data:{
                    tcId:$scope.oneClassId!=undefined?$scope.oneClassId:"",
                    scId:$scope.twoClassId,
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.pageSize,
                    authFlag:0
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel3();
                      
                })
            });

            $scope.color=1;
            $scope.showCOn=1;



            $scope.currentId=$scope.twoClassId=item.scId;
            var req ={
                type:"GET",
                uri:"getThirdCategory?scId="+item.scId,
                data:{}
            };
            httpServices.getcourseCata(req).success(function(response){
                $scope.$apply(function(){
                    $scope.ThreeList = response.data;
                   if(response.data.length != 0){
	                   	if($scope.showist==1){
	                   		$scope.triangleone=1;
	                   	}else{
	                   		$scope.triangleone=2;
	                   	}
						var top = $("#ThreeCourseSwitch").position().top;
						var width = $($(".seconClass")[index]).width()/2+$($(".seconClass")[index]).offset().left-$('#thrilistCon').offset().left;
						$(".triangle").css({"left":width,"top":top-8})
                   }
                })
            });


        };
        $scope.ThreeCourseSwitch=function(item){
            $scope.currentId3=item.dcId;
            var req ={
                type:"GET",
                uri:"getList",
                data:{
                    tcId:$scope.oneClassId,
                    scId:$scope.twoClassId,
                    thirdId:item.dcId,
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.pageSize,
                    authFlag:0
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    getListModel3();
                })
            });
        }
        /*点击全部加载默认列表*/
        /*排序*/
        $scope.isDisabled = false;
        $scope.peopecolor = false;
        $scope.DateColor = function(){
            $scope.isDisabled = !$scope.isDisabled;
            if($scope.isDisabled){
            	var req ={
                type:"GET",
                uri:"getList",
                data:{
                	tcId:$scope.tab!=undefined?$scope.tab:"",
                	scId:$scope.twoClassId!=undefined?$scope.twoClassId:"",
                	thirdId:$scope.currentId3!=undefined?$scope.currentId3:"",
                    keyWord:$scope.seacCon!=undefined?$scope.seacCon:"",
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    uploadTime:true,//下载排序
                    downloadCount: $scope.peopecolor,
                    authFlag:0
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;
                   
                    getListModel();
                })
            });
           
            }else{
            	
            	 var req ={
                type:"GET",
                uri:"getList",
                data:{
                	tcId:$scope.tab!=undefined?$scope.tab:"",
                	scId:$scope.twoClassId!=undefined?$scope.twoClassId:"",
                	thirdId:$scope.currentId3!=undefined?$scope.currentId3:"",
                    keyWord:$scope.seacCon!=undefined?$scope.seacCon:"",
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    uploadTime:false,//下载排序,
                    downloadCount: $scope.peopecolor,
                    authFlag:0
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    $scope.courseList = response.data.list;
                    $scope.startLevel = response.data.list.collectionCount;
                    $scope.totalItem = response.data.Pagination.totalCount;  
                    getListModel();
                })
            });
            }
           
        };
        
        $scope.peopleColor=function(){
     
            $scope.peopecolor = !$scope.peopecolor;
            if($scope.peopecolor){
	             var req ={
	                type:"GET",
	                uri:"getList",
	                data:{
	                	tcId:$scope.tab!=undefined?$scope.tab:"",
	                	scId:$scope.twoClassId!=undefined?$scope.twoClassId:"",
	                	thirdId:$scope.currentId3!=undefined?$scope.currentId3:"",
	                    keyWord:$scope.seacCon!=undefined?$scope.seacCon:"",
	                    pageNo:$scope.listParams.pageNo,
	                     pageSize:$scope.listParams.pageSize,
	                    downloadCount:true,//下载排序
	                    uploadTime:$scope.isDisabled,
                        authFlag:0
	                }
	            };
	            httpServices.getcourse(req).success(function(response){
	                $scope.$apply(function(){
	                    $scope.courseList = response.data.list;
	                    $scope.startLevel = response.data.list.collectionCount;
	                    $scope.totalItem = response.data.Pagination.totalCount;
	                    getListModel();
	                })
	            });
            }else{
            	var req ={
	                type:"GET",
	                uri:"getList",
	                data:{
	                	tcId:$scope.tab!=undefined?$scope.tab:"",
	                	scId:$scope.twoClassId!=undefined?$scope.twoClassId:"",
	                	thirdId:$scope.currentId3!=undefined?$scope.currentId3:"",
	                    keyWord:$scope.seacCon!=undefined?$scope.seacCon:"",	
	                    pageNo:$scope.listParams.pageNo,
	                    pageSize:$scope.listParams.pageSize,
	                    downloadCount:false,//下载排序
	                    uploadTime:$scope.isDisabled,
                        authFlag:0
	                }
	            };
	            httpServices.getcourse(req).success(function(response){
	                $scope.$apply(function(){
	                    $scope.courseList = response.data.list;
	                    $scope.startLevel = response.data.list.collectionCount;
	                    $scope.totalItem = response.data.Pagination.totalCount;
	                    getListModel();
	                })
	            });
            }
           
        }
        $scope.all=function(){
            $scope.tab=-1;
            $scope.showCOn=-1;
            $scope.showCOn2=-1;
            $scope.ThreeList="";
            $scope.ThreeSwitchDate="";
            sessionStorage.removeItem("topId")
            sessionStorage.removeItem("twoId")
            sessionStorage.removeItem("thrId")
            courseList();
            $scope.color = 2;
            $scope.currentId=0.1;
            $scope.currentId3 = 0.1;
            $scope.currentId2 = 0.1;
            $scope.currentId4 = 0.1;
        };
        $scope.saveSeachCOn=function(a){
            $scope.seacCon =a;
        }
        $scope.seachVIdeo=function(){
        	var req ={
	                type:"GET",
	                uri:"getList",
	                data:{
	                	tcId:$scope.tab!=undefined?$scope.tab:"",
	                	scId:$scope.twoClassId!=undefined?$scope.twoClassId:"",
	                	thirdId:$scope.currentId3!=undefined?$scope.currentId3:"",
	                    keyWord:$scope.seacCon,
	                    pageNo:$scope.listParams.pageNo,
	                    pageSize:$scope.listParams.pageSize,
                        authFlag:0
	                }
		            };
		            httpServices.getcourse(req).success(function(response){
		                $scope.$apply(function(){
		                    $scope.courseList = response.data.list;
		                    $scope.startLevel = response.data.list.collectionCount;
		                    $scope.totalItem = response.data.Pagination.totalCount;
		                    getListModel();
		                })
		            });
        	
        	
            
        }
        /*分页效果*/

        $scope.list = [];
        function getListModel(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage:$scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function (currentPage) {
                    if(currentPage){
                        $scope.paginationConf.currentPage = currentPage;
                    }
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                }
            };
        }

        function getListModel2(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage:$scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function (currentPage) {
                    if(currentPage){
                        $scope.paginationConf.currentPage = currentPage;
                    }
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                }
            };
        }
        function getListModel3(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage:$scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function (currentPage) {
                    if(currentPage){
                        $scope.paginationConf.currentPage = currentPage;
                    }
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    courseList();
                }
            };
        }


    })
});


